package edu.dillo.dillotude;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Player extends InteractiveObject {
	
	
	private boolean Touch = false;
	private boolean Topped = false;
	private float cameraPositionX, cameraPositionY, viewportX, viewportY;
	private int lr;
	private int none;
	private int s;
	
	public Player(float x, float y, float r) throws SlickException {
		
		super(x, y, r);		
		Velocity.x = 0;
		Velocity.y = 0;
		cameraPositionX= 0;
		cameraPositionY = 0;
		viewportX = 0;
		viewportY = 0;
		cameraPositionY =0;
		lr = 0;
		none = 0;
		s = 0;
	}

	public void update(GameContainer gc, StateBasedGame arg1, int arg2, GridBlock[][] GB)
			throws SlickException {
		
		float startx = X, starty = Y;
		int end2 = 1, fm;

		// this code isn't permanent, I will change this so it uses the acceleration vector,
		// but its fairly similar
		Velocity.set(Velocity.x+Acceleration.x, Velocity.y+Acceleration.y);

		// this is supposed to work with the acceleration vector, I need the code so it works with this properly.
		
		none = 1;
		if (gc.getInput().isKeyDown(Input.KEY_RIGHT) && Velocity.x < 10)
		{
			Acceleration.x = 1;
			activeAnimation = Animations.get("running");
		}
		else if (gc.getInput().isKeyDown(Input.KEY_LEFT) && Velocity.x > -10)
		{
			Acceleration.x = -1;
			activeAnimation = Animations.get("running-flipped");
		}
		else if(Velocity.x < 0)
		{
			Acceleration.x = 1;
		}
		else if(Velocity.x > 0)
		{
			Acceleration.x = -1;
		}
		else
		{
			Acceleration.x = 0;
			none = 0;
		}
		
		if (gc.getInput().isKeyDown(Input.KEY_UP) && Topped)
		{
			Velocity.y = -22;
			Animations.get("apex").restart();
			Animations.get("apex-flipped").restart();
			fm = 0;
		}
		else if(Velocity.y < 20 && !Topped)
		{
			Acceleration.y = 1;
		}
		else 
			Acceleration.y = 0;
		
		// this just tests the left and right keys input, and it also lowers velocity to zero if no keys are pressed
		// this is to simulate acceleration
		Topped = false;
		
		X += Velocity.x;
		Y += Velocity.y; 	
		
		for(int i = 0; i < GB.length; i++)
		{
			for (int j = 0 ; j < GB[i].length; j++)
			{
				Touch = TouchingBlock(GB[i][j]);
				if(Touch)
				{
					if(WhichSideofBlock(GB[i][j]) == dir.TOP)
					{
						Topped = true;
					}
					pushback(WhichSideofBlock(GB[i][j]), GB[i][j]);
				}
			}
		}
		
		if(Velocity.x > 0)
			lr = 0;
		else if(Velocity.x < 0)
			lr = 1;
		if((Animations.get("apex").getFrame() == Animations.get("apex").getFrameCount() - 1 )|| (Animations.get("apex-flipped").getFrame() == Animations.get("apex-flipped").getFrameCount() - 1 ))
			end2 = 0;
		//if(activeAnimation.getFrame() == activeAnimation.getFrameCount())
			//end2 = 0;
		
		
		
		if(Velocity.y > -5 && !Topped && end2 == 1 && lr == 0)
		{
			activeAnimation = Animations.get("apex");
			fm = activeAnimation.getFrame();
			if(s == 2)
			{
				fm = Animations.get("apex-flipped").getFrame();
				Animations.get("apex").setCurrentFrame(fm);
				System.out.println(activeAnimation.getFrame() + " " + fm);
			}
			s = 1;
		}
		else if(Velocity.y > -5 && !Topped && end2 == 1 && lr == 1)
		{
			activeAnimation = Animations.get("apex-flipped");
			
			if(s == 1)
			{
				fm = Animations.get("apex").getFrame();
				Animations.get("apex-flipped").setCurrentFrame(fm);
				System.out.println(activeAnimation.getFrame() + " " + fm);
			}
			s = 2;
			
		}
		else if(Velocity.y < 0 && lr == 0)
			activeAnimation = Animations.get("fly");
		else if(Velocity.y < 0 && lr == 1)
			activeAnimation = Animations.get("fly-flipped");
		else if(Velocity.y > 0 && lr == 0)
			activeAnimation = Animations.get("fall");
		else if(Velocity.y > 0 && lr == 1)
			activeAnimation = Animations.get("fall-flipped");
		else if(none == 0 && lr == 0)
			activeAnimation = Animations.get("idle");
		else if(none == 0 && lr == 1)
			activeAnimation = Animations.get("idle-flipped");
		
		// this tests if it touches, where it touches, and calls pushback()
		// this is what makes the character jumps, it tests if it is touching a ground piece
		// it doesn't test which side, so you could probably jump by touching the side
		// this can be fixed later
		
		//-------------setting the camera position-----------------
		float endx = X, endy = Y;
		float dx = endx - startx;
		float dy = endy -starty;
		float windowwidth = gc.getWidth();
		float windowheight = gc.getHeight();
		if ((X <= viewportX + 200) || (X >= (viewportX + windowwidth) - 400 ))
		{
			cameraPositionX += (-1 * dx);
			viewportX += dx;
		}
			
		if((Y >= (viewportY+windowheight) - 200) || (Y <= viewportY + 50))
		{
			cameraPositionY += (-1 * dy);
			viewportY += dy;
		}
			
		//-----------------------------------------------------------
	}
	public void render(GameContainer arg0, StateBasedGame arg1,Graphics g)
			throws SlickException {
		g.drawAnimation(activeAnimation, this.getX(), this.getY());
	}
	float getCameraPosititionX()
	{
		return cameraPositionX;
	}
	float getCameraPositionY()
	{
		return cameraPositionY;
	}
}

	
